home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / program / ccdl151s.zip / SOURCE / MEMMGT.C < prev    next >
C/C++ Source or Header  |  1997-03-15  |  5KB  |  220 lines

  1. /*
  2.  * 68K/386 32-bit C compiler.
  3.  *
  4.  * copyright (c) 1997, David Lindauer
  5.  * 
  6.  * This compiler is intended for educational use.  It may not be used
  7.  * for profit without the express written consent of the author.
  8.  *
  9.  * It may be freely redistributed, as long as this notice remains intact
  10.  * and either the original sources or derived sources 
  11.  * are distributed along with any executables derived from the originals.
  12.  *
  13.  * The author is not responsible for any damages that may arise from use
  14.  * of this software, either idirect or consequential.
  15.  *
  16.  * v1.35 March 1997
  17.  * David Lindauer, gclind01@starbase.spd.louisville.edu
  18.  *
  19.  * Credits to Mathew Brandt for original K&R C compiler
  20.  *
  21.  */
  22. /*
  23.  *memmgt.c
  24.  *
  25.  * memory management
  26.  */
  27. #include        <stdio.h>
  28. #include                <malloc.h>
  29.  
  30. void release_local(void);
  31. void release_global(void);
  32. void release_opt(void);
  33. void release_oc(void);
  34. void mem_summary(void);
  35.  
  36. typedef struct _blk_ {
  37.         struct _blk_      *next;
  38.                 long blksize;
  39.         char            m[1];           /* memory area */
  40.         } BLK;
  41.  
  42. int global_flag;
  43. static int      glbsize = 0,    /* size left in current global block */
  44.                 locsize = 0,    /* size left in current local block */
  45.                 glbindx = 0,    /* global index */
  46.                 locindx = 0;    /* local index */
  47.  
  48. static BLK       *locblk = 0,    /* pointer to local block */
  49.                         *glbblk = 0;    /* pointer to global block */
  50.  
  51. static int optsize,optindx,ocsize,ocindx;
  52. static BLK *optblk,*ocblk;
  53. static long maxopt,maxglb,maxloc,maxoc;
  54. void memini(void)
  55. {
  56.   global_flag = 1;
  57.     glbsize = 0;
  58.     locsize = 0;
  59.     glbindx = 0;
  60.     locindx = 0;
  61.     locblk = 0;
  62.     glbblk = 0;
  63.     optsize = 0;
  64.     optblk = 0;
  65.     optindx = 0;
  66.   maxopt = maxglb = maxloc = 0;
  67. }
  68. static char *palloc(int *sizepos,int size,int *indxpos,BLK **blk)
  69. /*
  70.  * main allocation routine
  71.  */
  72. {
  73.   BLK      *bp;
  74.   char            *rv;
  75.     if( size & 1 )        /* if odd size */
  76.         size += 1;    /* make it even */
  77.     /* if anything left, try to allocate from it */
  78.   if( *sizepos >= size ) {
  79.     rv = &((*blk)->m[*indxpos]);
  80.     *sizepos -= size;
  81.     *indxpos += size;
  82.     return rv;
  83.   }
  84.   else    {
  85.         long allocsize;
  86.         /* else check for size > normal blcok size */
  87.         if (size > 2048) {
  88.             /* this is going to fragment memory!!! I'd fix it except
  89.              * the fragmentation is partially dependent on the calloc 
  90.              * implementation 
  91.              */
  92.             allocsize = size - 1;
  93.             *sizepos = 0;
  94.         }
  95.         else {
  96.             /* as long as we stick to normal blocks, fragmentation
  97.              * won't be an issue because as long as all blocks are the
  98.              * same size calloc is guaranteed to find one if there are any
  99.              */
  100.             allocsize = 2047;
  101.             *sizepos = 2048 - size;
  102.         }
  103.         /* allocate mem */
  104.     bp = calloc(1,sizeof(BLK) + allocsize);
  105.         if( bp == NULL ) {
  106.             release_global();
  107.             release_local();
  108.             release_opt();
  109.             release_oc();
  110.             mem_summary();
  111.             fatal(" not enough memory.");
  112.         }
  113.         bp->blksize = allocsize;
  114.         /* link the block and return the base */
  115.     bp->next = *blk;
  116.     *blk = bp;
  117.     *indxpos = size;
  118.     return (*blk)->m;
  119.     }
  120. }
  121.  
  122. char    *xalloc(int siz)
  123. /*
  124.  * user-level allocation.  Global symbols are never deallocated; local
  125.  * symbols are deallocated all at once by deallocating the local symbol
  126.  * blocks
  127.  */
  128. {
  129.         if( global_flag ) 
  130.                         return palloc(&glbsize,siz,&glbindx,&glbblk);
  131.                 else
  132.                         return palloc(&locsize,siz,&locindx,&locblk);
  133. }
  134. char    *oalloc(int siz)
  135. /*
  136.  * allocation for optimizer temps
  137.  */
  138. {
  139. /*    return xalloc(siz); */
  140.                         return palloc(&optsize,siz,&optindx,&optblk);
  141. }
  142. char *ocalloc(int siz)
  143. /*
  144.  * Allocation for binary code gen
  145.  */
  146. {
  147.     return palloc(&ocsize,siz,&ocindx,&ocblk);
  148. }
  149. static long release(int *sizepos, BLK ** blk)
  150. /*
  151.  * msin memory free routine
  152.  * frees all blocks from a list at once
  153.  *
  154.  * This memory management scheme reduces fragmentation, however
  155.  * temps hang around for a while...
  156.  *
  157.  */
  158. {       BLK      *bp1, *bp2;
  159.                 long blkcnt = 0;
  160.         bp1 = *blk;
  161.         while( bp1 != 0 ) {
  162.                                 blkcnt+=bp1->blksize;
  163.                 bp2 = bp1->next;
  164.                 free( bp1 );
  165.                 bp1 = bp2;
  166.                 }
  167.         *blk = 0;
  168.         *sizepos = 0;
  169.                 return blkcnt;
  170. }
  171. void release_local(void )
  172. /*
  173.  * release all local allocations
  174.  */
  175. {
  176.     long temp = release(&locsize,&locblk);       
  177.     if (temp > maxloc)
  178.         maxloc = temp;
  179. }
  180.  
  181. void release_global(void)
  182. /*
  183.  * release all global allocations
  184.  */
  185.     long temp = release(&glbsize,&glbblk);       
  186.     if (temp > maxglb)
  187.         maxglb = temp;
  188. }
  189. void release_opt(void)
  190. /*
  191.  * release optimizer temps
  192.  */
  193. {
  194.     long temp = release(&optsize,&optblk);       
  195.     if (temp > maxopt)
  196.         maxopt = temp;
  197. }
  198. void release_oc(void)
  199. /*
  200.  * release all binary codegen allocations
  201.  */
  202.     long temp = release(&ocsize,&ocblk);       
  203.     if (temp > maxoc)
  204.         maxoc = temp;
  205. }
  206. void mem_summary(void)
  207. {
  208.     printf("Memory usage:\n");
  209.     if (maxglb)
  210.         printf("  Globals:        %ld\n",maxglb);
  211.     if (maxloc)
  212.         printf("  Local peak:     %ld\n",maxloc);
  213.     if (maxopt)
  214.         printf("  Optimizer peak: %ld\n",maxopt);
  215.     if (maxoc)
  216.         printf("  Binary code peak: %ld\n",maxopt);
  217.  
  218. }